package com.my.myupdata.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * 项目名称：跳转不同手机的权限设置页面
 * 创建人：mwb
 * 创建时间：2018/6/12 9:32
 */

public class IntentSettingUtil {
    private final String TAG = "mwb";
    private Context mContext;
    private String packageName = "com.my.myupdata";

    public IntentSettingUtil(Context context) {
        this.mContext = context;
    }

    public void jumpPermissionSetting() {
        String name = Build.MANUFACTURER;
        switch (name) {
            case "HUAWEI":
                goHuaWeiMainager();
                break;
            case "vivo":
                goVivoMainager();
                break;
            case "OPPO":
                goOppoMainager();
                break;
            case "Coolpad":
                goCoolpad();
                break;
            case "MeiZu":
                goMeiZuMainager();
                break;
            case "Xiaomi":
                goXiaoMiMainager();
                break;
            case "samsung":
                goSamSung();
                break;
            case "Sony":
                goSonyMainager();
                break;
            case "LG":
                goLGMainager();
                break;
            default:
                goIntentSetting();
                break;
        }
    }

    /**
     * 跳转普通的设置界面
     */
    private void goIntentSetting() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        Uri uri = Uri.fromParts("package", mContext.getPackageName(), null);
        intent.setData(uri);

        try {
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            Log.e(TAG, "e:" + e.toString());
        }
    }

    /**
     * 跳转LG的管理界面
     */
    private void goLGMainager() {
        try {
            Intent intent = new Intent(packageName);
            ComponentName componentName = new ComponentName("com.android.settings", "com.android.settings.Settings$AccessLockSummaryActivity");
            intent.setComponent(componentName);
            mContext.startActivity(intent);
        } catch (Exception e) {
            Log.e(TAG, "LG could not find permission ui");
            goIntentSetting();
        }
    }

    /**
     * 跳转索尼管理界面
     */
    private void goSonyMainager() {
        // 三星4.3可以直接跳转
        goIntentSetting();
    }

    /**
     * 跳转三星管理界面
     */
    private void goSamSung() {
    }

    /**
     * 跳转小米管理界面
     */
    private void goXiaoMiMainager() {
        String rom = getMiuiVersion();
        Intent intent = new Intent();
        if ("V6".equals(rom) || "V7".equals(rom)) {
            intent.setAction("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
            intent.putExtra("extra_pkgname", packageName);
        } else if ("V8".equals(rom) || "V9".equals(rom)) {
            intent.setAction("miui.intent.action.APP_PERM_EDITOR");
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            intent.putExtra("extra_pkgname", packageName);
        } else {
            goIntentSetting();
        }
        mContext.startActivity(intent);
    }

    private String getMiuiVersion() {
        String propName = "ro.miui.ui.version.name";
        String line = null;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(
                    new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return line;
    }

    /**
     * 跳转魅族管理界面
     */
    private void goMeiZuMainager() {
        try {
            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("packageName", packageName);
            mContext.startActivity(intent);
        } catch (Exception e) {
            goIntentSetting();
            Log.i(TAG, "MeiZu could not find permission ui");
        }
    }

    /**
     * 跳转Coolpad管理界面
     */
    private void goCoolpad() {
        doStartApplicationWithPackageName("com.yulong.android.security:remote");
    }

    /**
     * 跳转Oppo管理界面
     */
    private void goOppoMainager() {
        doStartApplicationWithPackageName("com.coloros.safecenter");
    }

    /**
     * 跳转Viov 的管理界面
     */
    private void goVivoMainager() {
        doStartApplicationWithPackageName(packageName);
    }

    /**
     * 通过包名获取此app的详细信息 包括activity、service、versioncode、name等等
     */
    private void doStartApplicationWithPackageName(String packageName) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = mContext.getPackageManager().getPackageInfo(packageName, 0);
        } catch (Exception e) {
            Log.e(TAG, "e:" + e.toString());
        }

        if (packageInfo == null) {
            return;
        }
        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveInent = new Intent(Intent.ACTION_MAIN, null);
        resolveInent.addCategory(Intent.CATEGORY_LAUNCHER);
        // 通过getPackageManager()的queryIntentActivitys方法遍历
        List<ResolveInfo> resolveInfoList = mContext.getPackageManager()
                .queryIntentActivities(resolveInent, 0);
        Log.e("PermissionPageManager", "resolveinfoList" + resolveInfoList.size());

        for (int i = 0; i < resolveInfoList.size(); i++) {
            Log.e("PermissionPageManager", resolveInfoList.get(i).activityInfo.packageName + resolveInfoList.get(i).activityInfo.name);
        }

        ResolveInfo resolveInfo = resolveInfoList.iterator().next();
        if (resolveInfo != null) {
            // packageName参数2 = 参数 packname
            packageName = resolveInfo.activityInfo.packageName;
            // 这个就是我们要找的该App的LAUNCHER的Activity[组织形式：packageName参数2.mainActivityname]
            String className = resolveInfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            // 设置ComponentName参数1：packageName参数2：MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);
            intent.setComponent(cn);

            try {
                mContext.startActivity(intent);
            } catch (Exception e) {
                Log.e(TAG, e.toString());
                goIntentSetting();
            }
        }
    }

    /**
     * 跳转到华为的管理器
     */
    private void goHuaWeiMainager() {
        try {
            Intent intent = new Intent(packageName);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName componentName = new ComponentName("com.huawei.systemmanager",
                    "com.huawei.permissionmanager.ui.MainActivity");
            intent.setComponent(componentName);
            mContext.startActivity(intent);
        } catch (Exception e) {
            goIntentSetting();
            Log.i(TAG, "HUAWEI could not find permission ui");
        }
    }
}
